package com.haierCamera.customapplication.ui.user.send;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.haierCamera.customapplication.BaseActivity;
import com.haierCamera.customapplication.R;
import com.haierCamera.customapplication.api.repo.UserRepo;
import com.haierCamera.customapplication.api.vo.GetSystemRecord;
import com.haierCamera.customapplication.databinding.HzklActivityUserMsgBinding;
import com.haierCamera.customapplication.databinding.HzklAdapterSendMsgBinding;
import com.haierCamera.customapplication.ui.custom.DataBoundViewHolder;
import com.haierCamera.customapplication.ui.user.WebNoticePublicActivity;
import com.haierCamera.customapplication.utils.android.HttpErrorProcess;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import dagger.Lazy;

public class HZKLSystemMsgActivity extends BaseActivity {
    private HzklActivityUserMsgBinding binding;
    private MyAdapter adapter;
    @Inject
    UserRepo repo;
    List<GetSystemRecord.Rows> mList = new ArrayList<>();
    @Inject
    Lazy<HttpErrorProcess> errorProcessLazy;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.hzkl_activity_user_msg);
        binding.text.setText("系统消息");
        initData();
    }

    int page = 1;

    private void initData() {
        binding.registerBack.setOnClickListener(v -> finish());
        adapter = new MyAdapter();
        binding.swipe.setOnRefreshListener(() -> getMsg(1));
        binding.msgRecycler.setAdapter(adapter);
        binding.msgRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    int offset = recyclerView.computeVerticalScrollOffset();
                    if (offset != 0 && recyclerView.computeVerticalScrollExtent() + offset >= recyclerView.computeVerticalScrollRange()) {
                        if (page == -1) {
                            Toast.makeText(HZKLSystemMsgActivity.this, "没有更多数据", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        page++;
                        getMsg(page);
                    }
                }
            }
        });
        getMsg(page);
    }

    private void getMsg(int page) {
        this.page = page;
        binding.swipe.setRefreshing(false);
        repo.getSystemMsg(page, 20).observe(this, getSystemResource -> {
            switch (getSystemResource.status) {
                case LOADING:
                    showLoadingDialog();
                    break;
                case ERROR:
                    dismissLoadingDialog();
                    //Toast.makeText(this, getSystemResource.errorMessage, Toast.LENGTH_SHORT).show();
                    errorProcessLazy.get().process(getSystemResource);
                    break;
                case SUCCESS:
                    dismissLoadingDialog();
                    if (page == 1) {
                        mList.clear();
                    }
                    GetSystemRecord data = getSystemResource.data;
                    assert data != null;
                    if (data.total == 0) {
                        Toast.makeText(this, "暂无数据", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (page > data.pagecount) {
                        this.page = -1;
                        Toast.makeText(this, "没有更多数据", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    List<GetSystemRecord.Rows> rows = data.rows;
                    mList.addAll(rows);
                    adapter.upDate(mList);
                    break;
            }
        });
    }

    class MyAdapter extends RecyclerView.Adapter<DataBoundViewHolder<HzklAdapterSendMsgBinding>> {
        List<GetSystemRecord.Rows> mList;

        public void upDate(List<GetSystemRecord.Rows> mList) {
            this.mList = mList;
            notifyDataSetChanged();
        }

        @NonNull
        @Override
        public DataBoundViewHolder<HzklAdapterSendMsgBinding> onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            HzklAdapterSendMsgBinding binding = DataBindingUtil.inflate(LayoutInflater.from(HZKLSystemMsgActivity.this), R.layout.hzkl_adapter_send_msg, parent, false);
            return new DataBoundViewHolder<>(binding);
        }

        @Override
        public void onBindViewHolder(@NonNull DataBoundViewHolder<HzklAdapterSendMsgBinding> holder, int position) {
            holder.binding.title.setText(mList.get(position).title);
            holder.binding.time.setText(mList.get(position).time);
            holder.binding.icon.setImageResource(R.drawable.msg_icon2);
            Log.i("TAG", "url-" + mList.get(position).content);
            holder.itemView.setOnClickListener(view ->
                    startActivity(new Intent(HZKLSystemMsgActivity.this
                            , WebNoticePublicActivity.class)
                            .putExtra("title", mList.get(position).title)
                            .putExtra("time", mList.get(position).time)
                            .putExtra("url", mList.get(position).content)
                    ));
        }

        @Override
        public int getItemCount() {
            return null == mList ? 0 : mList.size();
        }


    }
}
